package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @Author: haiyuny
 * @Date: 2022/7/29 15:33
 * @Description:
 */
@RestController
@RequestMapping("shopping")
public class CartController {


    @Autowired
    ICartService iCartService;

    /**
     * @param [userInfo]
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author 章鹏
     * @description 获得购物车商品列表
     * @date 2022/7/29 17:13
     */
    @GetMapping("carts")
    public ResponseData getCartListById(HttpServletRequest request) throws JsonProcessingException {
        String userInfo = request.getHeader("user_info");
        Map map = JSON.parseObject(userInfo, Map.class);
        //获得uid
        Integer uid1 = (Integer) map.get("uid");
        int  uid2 = uid1;
        Long uid= (long) uid2;

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);
        CartListByIdResponse cartListByIdResponse = iCartService.getCartListById(cartListByIdRequest);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(cartListByIdResponse.getCode())) {
            return new ResponseUtil().setData(cartListByIdResponse.getCartProductDtos());
        }
        //失败
        return new ResponseUtil().setErrorMsg(cartListByIdResponse.getMsg());
    }


    /**
     * @param [request]
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author 章鹏
     * @description 添加商品到购物车
     * @date 2022/7/29 17:22
     */
    @PostMapping("carts")
    public ResponseData addToCart(@RequestBody AddCartRequest request) {
        AddCartResponse addCartResponse = iCartService.addToCart(request);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        //失败
        return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
    }

    /**
     * @param [request]
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author 章鹏
     * @description 更新购物车中商品的数量
     * @date 2022/7/29 17:24
     */
    @PutMapping("carts")
    public ResponseData updateCartNum(@RequestBody UpdateCartNumRequest request) {
        UpdateCartNumResponse updateCartNumResponse = iCartService.updateCartNum(request);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(updateCartNumResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        //失败
        return new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());
    }


    @PutMapping("items")
    public ResponseData checkedAllItemsOr(@RequestBody CartForm cartForm) {


        CheckAllItemRequest request = new CheckAllItemRequest();
        request.setUserId(cartForm.getUserId());
        request.setChecked(cartForm.getChecked());

        CheckAllItemResponse checkAllItemResponse = iCartService.checkAllCartItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(checkAllItemResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(checkAllItemResponse.getMsg());

    }

    @DeleteMapping("items/{uid}")
    public ResponseData deleteCheckedCartItems(@PathVariable("uid") Long uid) {

        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);

        DeleteCheckedItemResposne deleteCheckedItemResposne = iCartService.deleteCheckedItem(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCheckedItemResposne.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(deleteCheckedItemResposne.getMsg());
    }

    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCarts(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {

        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setItemId(pid);
        request.setUserId(uid);
        DeleteCartItemResponse deleteCartItemResponse = iCartService.deleteCartItem(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCartItemResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());
    }

}
